package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanSocialQO;
import com.xingkeduo.pojo.table.CraftsmanSocialTO;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.*;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 一线员工个人信息封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:26:45
 * @description
 */
@Component
public class CraftsmanSocialJTO extends BaseJson {

    public static CraftsmanSocialTO createTableForModify(String json) throws Throwable {
        CraftsmanSocialTO table = new CraftsmanSocialTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setName(validName(root, YesNoEnum.NO)).setIdCard(validIdCard(root, YesNoEnum.NO))
                .setPhone(validPhone(root, YesNoEnum.NO)).setSex(validSex(root, YesNoEnum.NO))
                .setBirth(validBirth(root, YesNoEnum.NO)).setEthnic(validEthnic(root, YesNoEnum.NO))
                .setDegree(validDegree(root, YesNoEnum.NO)).setIsMarried(validIsMarried(root, YesNoEnum.NO))
                .setHouseHold(validHouseHold(root, YesNoEnum.NO))
                .setHouseHoldType(validHouseHoldType(root, YesNoEnum.NO)).setBank(validBankValue(root, YesNoEnum.NO))
                .setBankCard(validBankCard(root, YesNoEnum.NO)).setSubBranch(validString(root, "subbranch", 50, YesNoEnum.NO));

        return table;
    }

    public static CraftsmanSocialQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));
        return query;
    }

    public static CraftsmanSocialQO createQueryForPhone(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setPhone(validString(root, "phone", 11, YesNoEnum.YES));
        if (!RegularUtil.isSelfPhone(query.getPhone())) {
            throw new DataException(DataException.Type.DEFAULT, "手机号格式不正确");
        }
        query.setCode(validCode(root, YesNoEnum.NO)).setId(validId(root, YesNoEnum.NO)).setType(validType(root, YesNoEnum.NO));
        return query;
    }

    public static CraftsmanSocialQO createQueryForListByCode(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setName(validName(root, YesNoEnum.NO)).setPhone(validPhone(root, YesNoEnum.NO));
        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            query.setName(condition);
        }
        return query;
    }

    public static CraftsmanSocialQO createQueryForList(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setName(validName(root, YesNoEnum.NO)).setPhone(validPhone(root, YesNoEnum.NO));
        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            if (condition.matches("\\d+")) {
                if (RegularUtil.isSelfPhone(condition)) {
                    query.setPhone(condition);
                } else {
                    query.setId(Long.parseLong(condition));
                }
            } else {
                query.setName(condition);
            }
        }
        return query;
    }

    public static CraftsmanSocialQO createQueryForConditionByCode(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            if (!condition.matches("\\d+")) {
                query.setName(condition);
            }
        }
        return query;
    }

    /**
     * @param json:包含查询条件姓名,手机，id
     * @return : com.xingkeduo.pojo.query.CraftsmanSocialQO
     * @author : gaozifeng
     * @date : 2019/3/4 17:21
     */
    public static CraftsmanSocialQO createConditionList(String json) throws Throwable {
        CraftsmanSocialQO query = new CraftsmanSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;
        query.setSex(validSex(root, YesNoEnum.NO)).setCondition(validCondition(root, YesNoEnum.NO));
        return query;
    }

    public static ObjectNode toJson(CraftsmanSocialTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            if (table.getName() != null)
//                root.put("name", table.getName());
                root.put("name", table.getName());
            if (table.getIdCard() != null)
                root.put("idcard", table.getIdCard());
            if (table.getPhone() != null)
                root.put("phone", table.getPhone());
            root.put("age", "-");
            root.put("wechatName", Base64Utils.decoderParam(table.getWechatName()));
            root.put("signaTure", table.getSignaTure() == null ? "" : table.getSignaTure());
            if (null != table.getIdCard()) {
                if (table.getIdCard().length() == 18 || table.getIdCard().length() == 15) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                    String sexStr = table.getIdCard().substring(16, 17);
//                    table.setSex(SexEnum.getSex(Integer.parseInt(sexStr) % 2));
                    String birthStr = table.getIdCard().substring(6, 10);
                    String nowTime = sdf.format(new Date());
                    root.put("age", Integer.valueOf(nowTime) - Integer.valueOf(birthStr));
                }
            }
            if (table.getSex() != null) {
                root.put("sex_value", table.getSex().getValue());
                root.put("sex_desc", table.getSex().getDesc());
            }
            if (table.getBirth() != null) {
                SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd");
                root.put("birth", formatDateTime.format(table.getBirth()));
            }
        }
        if (table.getEthnic() != null)
            root.put("ethnic", table.getEthnic());
        if (table.getDegree() != null) {
            root.put("degree_value", table.getDegree().getValue());
            root.put("degree_desc", table.getDegree().getDesc());
        }
        if (table.getIsMarried() != null) {
            root.put("ismarried_value", table.getIsMarried().getValue());
            root.put("ismarried_desc", table.getIsMarried().getDesc());
        }
        if (table.getBankCard() != null) {
            root.put("bankcard", table.getBankCard());
        } else {
            root.put("bankcard", "-");
        }
        if (table.getBank() != null) {
            root.put("bank_desc", table.getBack().getDesc());
            root.put("bank_value", table.getBack().getValue());
        } else {
            root.put("bank", "-");
            root.put("bank_value", "-");
        }
        if (table.getSubBranch() != null) {
            root.put("subBranch", table.getSubBranch());
        } else {
            root.put("subBranch", "-");
        }
        root.put("household", table.getHouseHold());
        if (table.getHouseHoldType() != null) {
            root.put("householdtype_value", table.getHouseHoldType().getValue());
            root.put("householdtype_desc", table.getHouseHoldType().getDesc());
        } else {
            root.put("householdtype_value", "-");
            root.put("householdtype_desc", "-");
        }
        toBaseJson(root, table);
        return root;
    }


    public static ArrayNode toJson(List<CraftsmanSocialTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<CraftsmanSocialTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJsonForShop(iterator.next()));
            }

        return root;
    }

    /**
     * 返回id(个数)和name
     *
     * @param table:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/3/30 15:09
     */
    public static ObjectNode toJsonForShop(CraftsmanSocialTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            if (table.getName() != null)
                root.put("name", table.getName());
        }
        toBaseJson(root, table);
        return root;
    }

    public static String validName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "name";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }


    public static int validType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "type";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Integer.valueOf(value);
        }
        return 0;
    }

    public static String validPhone(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "phone";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isSelfPhone(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validIdCard(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "idcard";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isDefaultIdCardNo(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static SexEnum validSex(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "sex";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!SexEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return SexEnum.getSex(value);
        }
        return null;
    }

    public static Date validBirth(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "birth";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isDateTime(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(value);
        }
        return null;
    }

    public static String validEthnic(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "ethnic";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!CommonUtil.isEmpty(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static DegreeEnum validDegree(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "degree";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!DegreeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return DegreeEnum.getDegree(value);
        }
        return null;
    }

    public static YesNoEnum validIsMarried(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "ismarried";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }

    public static String validHouseHold(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "household";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 30)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static HouseHoldTypeEnum validHouseHoldType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "householdtype";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!HouseHoldTypeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return HouseHoldTypeEnum.getHouseHoldType(value);
        }
        return null;
    }

    public static String validBankCard(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "bankcard";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 19)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validCondition(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "condition";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (null == value || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }


    public static BankEnums validBankValue(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "bank";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!BankEnums.validValue(value) || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return BankEnums.getBankValue(value);
        }
        return null;
    }

}
